﻿using Solidinfo.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace Solidinfo.Web.Helpers
{
    public static class EnumHtmlHelper
    {

        /// <summary>
        /// 获取枚举的SelectListItems
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <returns>结果</returns>
        public static List<SelectListItem> GetListItems<T>() where T : struct
        {
            var enumItems = EnumHelper.GetAllEnumItems<T>();
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (var item in enumItems)
            {
                SelectListItem listItem = new SelectListItem()
                {
                    Value = ((IEnumItem)item).Value.ToString(),
                    Text = item.DisplayText
                };
                list.Add(listItem);
            }
            return list;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="textFiledName">要修改的目标列</param>
        /// <param name="keyFiledName">获取语言Key的列</param>
        /// <returns></returns>
        public static List<T> _ToLocal<T>(this List<T> list, string textFiledName, string keyFiledName)
        {
            PropertyInfo[] propertys = typeof(T).GetProperties();

            foreach (var  item in list)
            {
                object value=null;
                foreach (PropertyInfo pi in propertys)
                {
                    if (pi.Name == keyFiledName) {                       
                        value = MultilingualHelper.GetMultilingualString(pi.GetValue(item, null).ToString());
                    }
                }
                foreach (PropertyInfo pi in propertys)
                {
                    if (pi.Name == textFiledName)
                    {
                        pi.SetValue(item, value, null);
                    }
                }
            }

            return list;



        }
    }
}